package com.neteast.androidclient.newscenter.domain;

import java.nio.ByteBuffer;
import java.util.Arrays;

import com.neteast.androidclient.newscenter.Global;

public class BootStrapRequest {
	public byte cmd=Global.NET_BOOTSTRAP_REQUEST;
	/** 通信协议 */
	public int protocolVersion;// 
	/** 用户id，guest为0*/
	public int userId=0;
	/** 消息盒子版本号 长度10 */
	public byte[] clientVersion;//
	/** 终端类型描述   长度10 */
	public byte[] terminalVersion;//
	/** 报文id */
	public int msgId;//
	/**保留字段长度  */
	public int reserveLen;
	/**保留字段  */
	public byte[] reserve;
	
	/**
	 * 
	 * @param cmd NET_BOOTSTRAP_REQUEST
	 * @param protocol_version 通信协议
	 * @param userId 用户id，guest为0
	 * @param client_version  消息盒子版本号 长度10
	 * @param terminal_version 终端类型描述   长度10
	 * @param msgid 报文id
	 * @param reserve_len 保留字段长度
	 */
	public BootStrapRequest(int protocolVersion,int userId, byte[] clientVersion,byte[] terminalVersion, int msgId) {
		super();
		this.protocolVersion = protocolVersion;
		this.userId=userId;
		this.clientVersion = clientVersion;
		this.terminalVersion = terminalVersion;
		this.msgId = msgId;
		this.reserveLen = 0;
	}
	
	/**
	 * 
	 * @param cmd NET_BOOTSTRAP_REQUEST
	 * @param protocol_version 通信协议
	 * @param userId 用户id，guest为0
	 * @param client_version  消息盒子版本号 长度10
	 * @param terminal_version 终端类型描述   长度10
	 * @param msgid 报文id
	 * @param reserve_len 保留字段长度
	 * @param reserve byte[] 保留字段 
	 */
	public BootStrapRequest(int protocolVersion, int userId,byte[] clientVersion, byte[] terminalVersion, int msgId,
			int reserveLen, byte[] reserve) {
		super();
		this.userId = userId;
		this.clientVersion = clientVersion;
		this.terminalVersion = terminalVersion;
		this.msgId = msgId;
		this.reserveLen = reserveLen;
		this.reserve = reserve;
	}


	/**
	 * 获得通信报文
	 * @return
	 */
	public byte[] getMessage(){
		ByteBuffer buffer=ByteBuffer.allocate(37+reserveLen);
		buffer.put(cmd);
		buffer.putInt(protocolVersion);
		buffer.putInt(userId);
		buffer.put(clientVersion);
		buffer.put(terminalVersion);
		buffer.putInt(msgId);
		buffer.putInt(reserveLen);
		if (reserveLen>0) {
			buffer.put(reserve);
		}
		buffer.flip();
		return buffer.array();
	}

	@Override
	public String toString() {
		if (reserve!=null) {
			return "BootStrapRequest [cmd=" + cmd + ", protocolVersion="
					+ protocolVersion + ", userId=" + userId + ", clientVersion="
					+ Arrays.toString(clientVersion) + ", terminalVersion="
					+ Arrays.toString(terminalVersion) + ", msgId=" + msgId
					+ ", reserveLen=" + reserveLen + ", reserve="
					+ Arrays.toString(reserve) + "]";
		}else {
			return "BootStrapRequest [cmd=" + cmd + ", protocolVersion="
					+ protocolVersion + ", userId=" + userId + ", clientVersion="
					+ Arrays.toString(clientVersion) + ", terminalVersion="
					+ Arrays.toString(terminalVersion) + ", msgId=" + msgId
					+ ", reserveLen=" + reserveLen +"]";
		}
	}
}
